package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthUserDTO;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.cert.Extension;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private AuthUserDomainService authUserDomainService;
    /**
     * 用户注册
     * @param authUserDTO
     * @return
     */
    @RequestMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("UserController.register.authUserDTO:{}", JSON.toJSONString(authUserDTO));
            }
            checkUserInfo(authUserDTO);
            AuthUserBO authUserBO= AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok( authUserDomainService.register(authUserBO));
        }catch (Exception e){
            log.error("UserController.register.error:{}",e.getMessage(),e);
            return Result.fail("注册用户失败");
        }

    }

    /**
     * 修改用户信息
     */
    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("UserController.update.authUserDTO:{}",JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getId(),"用户id不能为空");
            checkUserInfo(authUserDTO);
            AuthUserBO authUserBO=AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        }catch (Exception e){
            log.error("UserController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新用户信息失败");
        }
    }
    public void checkUserInfo(@RequestBody AuthUserDTO authUserDTO){
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()),"用户名不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getPassword()),"密码不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getEmail()),"邮箱地址不能为空");
    }

    /**
     *删除用户
     * @return
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("UserController.delete.authUserDTO:{}",JSON.toJSONString(authUserDTO));
            }
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.delete(authUserBO));
        }catch (Exception e){
            log.error("UserController.delete.error:{}",e.getMessage(),e);
            return Result.fail("用户删除失败");
        }
    }

    /**
     * 用户启用/禁用
     * @param authUserDTO
     * @return
     */
    @RequestMapping("changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO){
        try{
            if(log.isInfoEnabled()){
                log.info("UserController.changeStatus.authUserDTO:{}",JSON.toJSONString(authUserDTO));
            }
            Preconditions.checkNotNull(authUserDTO.getStatus(),"用户状态不能为空");
            AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE
                    .convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        }catch (Exception e){
            log.error("UserController.changeStatus.error:{}",e.getMessage(),e);
            return Result.fail("用户启用/禁用失败");
        }
    }

    /**
     * 微信登录，关注公众号，发送验证码获取四位数，然后传入
     * @param validCode
     * @return
     */
    @RequestMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try{
            if(log.isInfoEnabled()){
                log.info("UserController.doLogin.validCode:{}",JSON.toJSONString(validCode));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(validCode),"验证码不能为空!");
            return Result.ok(authUserDomainService.dologin(validCode));
        }catch (Exception e){
            log.error("UserController.doLogin.error:{}",e.getMessage(),e);
            return Result.fail("用户登录失败");
        }


    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
